﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using CustomersSuppliersBusinessLayer;
using CustomersSuppliersDataLayer;
using System.Collections;
using WebHelper.Helpers;

namespace CustomersSuppliersWebApplication
{
    public partial class WebFormProducts : WebFormBase
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                InitializeTelerikRequest(this.RadTextBoxSearch, "onkeyup");
                this.PageTitle = "المنتجات";
                this.getProductsClassificationsFilter();
            }
        }
        protected void RadTextBoxSearch_TextChanged(object sender, EventArgs e)
        {
            this.getProducts();
            this.RadGridResults.Rebind();
        }
        protected void RadComboBox_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            this.getProducts();
            this.RadGridResults.Rebind();
        }
        protected void RadGridResults_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            this.getProducts();
        }
        protected void RadGridResults_ItemCreated(object sender, GridItemEventArgs e)
        {
            //pager
            if (e.Item is GridPagerItem)
            {
                GridPagerItem item = e.Item as GridPagerItem;
                Button changePageSize = (Button)e.Item.FindControl("ChangePageSizeLinkButton");
                changePageSize.Text = "تغيير";
                Button goTO = (Button)e.Item.FindControl("GoToPageLinkButton");
                goTO.Text = "تغيير";
                Label itesNumberLabel = (Label)e.Item.FindControl("ChangePageSizeLabel");
                itesNumberLabel.Text = "          عدد الصفوف :";
                Label goTOLabel = (Label)e.Item.FindControl("GoToPageLabel");
                goTOLabel.Text = "صفحة :";
                Label ofLabel = (Label)e.Item.FindControl("PageOfLabel");
                ofLabel.Text = "من " + item.Paging.PageCount.ToString();
            }
        }
        protected void RadGridResults_ItemCommand(object sender, GridCommandEventArgs e)
        {
            //set default value
            if (e.CommandName == RadGrid.InitInsertCommandName)
            {
                //e.Canceled = true;
                //System.Collections.Specialized.ListDictionary newValues = new System.Collections.Specialized.ListDictionary();
                //newValues[BLProduct.PropertiesNames.Product_StartDate] = DateTime.Now.Date;
                //e.Item.OwnerTableView.InsertItem(newValues);
            }

        }
        protected void RadGridResults_InsertCommand(object sender, GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            Hashtable newValues = new Hashtable();
            e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
            BLProduct Product = new BLProduct();
            if (newValues[BLProduct.PropertiesNames.Product_Code] != null)
            {
                Product.Product_Code = newValues[BLProduct.PropertiesNames.Product_Code].ToString();
            }
            if (newValues[BLProduct.PropertiesNames.Product_NameAr] != null && newValues[BLProduct.PropertiesNames.Product_NameAr].ToString().Trim() != "")
            {
                Product.Product_NameAr = newValues[BLProduct.PropertiesNames.Product_NameAr].ToString();
            }
            else
            {
                this.ShowNotification("من فضلك قم بادخال الاسم", NotificationIconType.Warning);
                e.Canceled = true;
                return;
            }
            if (newValues[BLProduct.PropertiesNames.Product_NameEn] != null)
            {
                Product.Product_NameEn = newValues[BLProduct.PropertiesNames.Product_NameEn].ToString();
            }
            if (newValues[BLProduct.PropertiesNames.Product_Notes] != null)
            {
                Product.Product_Notes = newValues[BLProduct.PropertiesNames.Product_Notes].ToString();
            }
            RadComboBox radComboBoxProductsClassifications = (RadComboBox)editedItem.FindControl("RadComboBoxProductsClassifications");
            if (radComboBoxProductsClassifications.SelectedValue != "")
            {
                Product.ProductClassification_ID = new Guid(radComboBoxProductsClassifications.SelectedValue);
            }

            Product.Product_Search = StringHelper.ToUniqueStatement(Product.Product_Code + Product.Product_NameAr + Product.Product_NameEn + Product.Product_Notes);
            Product.AutoSave();
            if (Product.Error == null)
            {
                this.ShowNotification("تم الحفظ بنجاح", NotificationIconType.Ok);
            }
            else
            {
                this.ShowNotification("حدث خطا اثناء الحفظ", NotificationIconType.Error);
                e.Canceled = true;
                return;
            }
        }
        protected void RadGridResults_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            Hashtable newValues = new Hashtable();
            e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
            BLProduct Product = new BLProduct();
            Product.Product_ID = new Guid(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex][BLProduct.PropertiesNames.Product_ID].ToString());
            Product.LoadByIdentifiers();
            if (newValues[BLProduct.PropertiesNames.Product_Code] != null)
            {
                Product.Product_Code = newValues[BLProduct.PropertiesNames.Product_Code].ToString();
            }
            if (newValues[BLProduct.PropertiesNames.Product_NameAr] != null && newValues[BLProduct.PropertiesNames.Product_NameAr].ToString().Trim() != "")
            {
                Product.Product_NameAr = newValues[BLProduct.PropertiesNames.Product_NameAr].ToString();
            }
            else
            {
                this.ShowNotification("من فضلك قم بادخال الاسم", NotificationIconType.Warning);
                e.Canceled = true;
                return;
            }
            if (newValues[BLProduct.PropertiesNames.Product_NameEn] != null)
            {
                Product.Product_NameEn = newValues[BLProduct.PropertiesNames.Product_NameEn].ToString();
            }
            if (newValues[BLProduct.PropertiesNames.Product_Notes] != null)
            {
                Product.Product_Notes = newValues[BLProduct.PropertiesNames.Product_Notes].ToString();
            }
            RadComboBox radComboBoxProductsClassifications = (RadComboBox)editedItem.FindControl("RadComboBoxProductsClassifications");
            if (radComboBoxProductsClassifications.SelectedValue != "")
            {
                Product.ProductClassification_ID = new Guid(radComboBoxProductsClassifications.SelectedValue);
            }
            Product.Product_Search = StringHelper.ToUniqueStatement(Product.Product_Code + Product.Product_NameAr + Product.Product_NameEn + Product.Product_Notes);
            Product.AutoSave();
            if (Product.Error == null)
            {
                this.ShowNotification("تم الحفظ بنجاح", NotificationIconType.Ok);
            }
            else
            {
                this.ShowNotification("حدث خطا اثناء الحفظ", NotificationIconType.Error);
                e.Canceled = true;
                return;
            }
        }
        protected void RadGridResults_DeleteCommand(object sender, GridCommandEventArgs e)
        {
            BLProduct Product = new BLProduct();
            Product.Product_ID = new Guid(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex][BLProduct.PropertiesNames.Product_ID].ToString());
            Product.DeleteByIdentifiers();
            if (Product.Error == null)
            {
                this.ShowNotification("تم الحذف بنجاح", NotificationIconType.Ok);
            }
            else
            {
                this.ShowNotification("حدث خطا اثناء الحذف", NotificationIconType.Error);
                return;
            }
        }
        protected void RadGridResults_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            {
                //combobox ProductClassificatio
                RadComboBox radComboBoxProductsClassifications = (RadComboBox)e.Item.FindControl("RadComboBoxProductsClassifications");
                radComboBoxProductsClassifications.DataSource = CashVariables.ProductsClassificationCollection;
                radComboBoxProductsClassifications.DataTextField = BLProductsClassification.PropertiesNames.ProductClassification_NameAr;
                radComboBoxProductsClassifications.DataValueField = BLProductsClassification.PropertiesNames.ProductClassification_ID;
                radComboBoxProductsClassifications.DataBind();
                if (e.Item.ItemIndex != -1)
                {
                    BLProduct Product = (BLProduct)e.Item.DataItem;
                    radComboBoxProductsClassifications.SelectedValue = Product.ProductClassification_ID.ToString();
                }


                //width
                GridEditableItem editedItem = e.Item as GridEditableItem;
                GridEditManager editMan = editedItem.EditManager;
                GridTextBoxColumnEditor editor = (GridTextBoxColumnEditor)(editMan.GetColumnEditor(BLProduct.PropertiesNames.Product_Notes));
                editor.TextBoxMode = TextBoxMode.MultiLine;
                editor.TextBoxControl.Width = Unit.Pixel(300);
                editor.TextBoxControl.Height = Unit.Pixel(40);
            }
        }

        private void getProducts()
        {
            var items = from i in CashVariables.ProductCollection
                        where i.Product_Search.Contains(StringHelper.ToUniqueStatement(this.RadTextBoxSearch.Text))
                        select i;
            if (this.RadComboBoxProductsClassifications.SelectedValue == "11111111-1111-1111-1111-111111111111")
            {
            }
            else if (this.RadComboBoxProductsClassifications.SelectedValue == "00000000-0000-0000-0000-000000000000")
            {
                items = from j in items where j.ProductClassification_ID == null select j; 
            }
            else
            {
                items = from k in items  where k.ProductClassification_ID==new Guid (this.RadComboBoxProductsClassifications.SelectedValue) select k;
            }
            BLProductCollection ProductCollection = new BLProductCollection(items);
            for (int i = 0; i < ProductCollection.Count; i++)
            {
                int index = CashVariables.ProductsClassificationCollection.FindByIentifier(ProductCollection[i].ProductClassification_ID);
                if (index != -1)
                {
                    ProductCollection[i].ProductClassification_NameAr = CashVariables.ProductsClassificationCollection[index].ProductClassification_NameAr;
                }
            }

            this.RadGridResults.DataSource = ProductCollection;
            this.RadGridResults.MasterTableView.DataKeyNames = BLProduct.PropertiesNames.Identifiers;
        }
        private void getProductsClassificationsFilter()
        {
            var items = (from i in CashVariables.ProductsClassificationCollection select i);
            BLProductsClassificationCollection ProductsClassificationCollection = new BLProductsClassificationCollection(items);
            ProductsClassificationCollection.Insert(0, new BLProductsClassification { ProductClassification_ID = new Guid("00000000-0000-0000-0000-000000000000"), ProductClassification_NameAr = "بدون" });
            ProductsClassificationCollection.Insert(0, new BLProductsClassification { ProductClassification_ID = new Guid("11111111-1111-1111-1111-111111111111"), ProductClassification_NameAr = "الكل" });
            this.RadComboBoxProductsClassifications.DataSource = ProductsClassificationCollection;
            this.RadComboBoxProductsClassifications.DataTextField = BLProductsClassification.PropertiesNames.ProductClassification_NameAr;
            this.RadComboBoxProductsClassifications.DataValueField = BLProductsClassification.PropertiesNames.ProductClassification_ID;
            this.RadComboBoxProductsClassifications.DataBind();
        }

    }
}